package fi.roose.uidesignia.ui;

import fi.roose.uidesignia.R;
import fi.roose.uidesignia.data.DataModel;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Logon extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.logon);

		final EditText groupName = (EditText) findViewById(R.id.group_id_edittect);
		final Button joinButton = (Button) findViewById(R.id.join_group_button);
		final Button createButton = (Button) findViewById(R.id.create_group_button);

		joinButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String name = addGroup(groupName);
				Toast.makeText(Logon.this, "Joined " + name, Toast.LENGTH_LONG).show();
				finish();
			}
		});

		createButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String name = addGroup(groupName);
				Toast.makeText(Logon.this, "Created " + name, Toast.LENGTH_LONG).show();
				finish();
			}
		});
	}

	public String addGroup(EditText edit) {
		String name = edit.getText().toString().trim();
		if (name == null || name.isEmpty()) {
			return null;
		} else if (!name.startsWith("#")) {
			name = "#" + name;
		}

		setResult(RESULT_OK);
		DataModel.addGroup(name);
		return name;
	}

	@Override
	public void onBackPressed() {
		setResult(RESULT_CANCELED);
		finish();
	}

}